package com.example.studyproject.thread;

import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

/**
 * @program: StudyProject
 * @description:
 * @author: kangwei
 * @create: 2025-07-03 16:24
 **/
@Component
public class ThreadPoolCommonService implements ExecutorWrapper {
    @Override
    public <E extends ExecutorService> void execute(E service, CallExecute runExecute) {
        service.execute(runExecute::call);
    }

    @Override
    public <E extends ExecutorService> Future<?> submit(E service, CallExecute runExecute) {
        return service.submit(runExecute::call);
    }

    @Override
    public <E extends ExecutorService> void submitV1(E service, CallExecute runExecute) {
        service.submit(runExecute::call);
    }

    @Override
    public <E extends ExecutorService> List<Future> invokeAll(E service, Collection tasks) {

        try {
            return service.invokeAll(tasks);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public <E extends ExecutorService> List<Future> invokeAll(E service, Collection tasks, long timeout, TimeUnit unit) {
        // TODO
        try {
            return service.invokeAll(tasks);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}
